from django.db import models
from django.contrib.auth.models import User
from taggit.managers import TaggableManager

# 媒体类型
MEDIA_TYPE_LIST = [
    ('视频', '视频'),
    ('音频', '音频'),
    ('图片', '图片'),
    ('其他', '其他')
]
# 媒体分类
MEDIA_CATEGORY_LIST = [
    ('事业单位', '事业单位'),
    ('推荐课', '推荐课'),
    ('银行', '银行'),
    ('教师', '教师'),
    ('医疗', '医疗'),
    ('招警考试(警察/辅警)', '招警考试(警察/辅警)'),
    ('三支一扶', '三支一扶'),
    ('国考', '国考'),
    ('省考', '省考'),
    ('选调生', '选调生'),
    ('电力招考', '电力招考'),
    ('蒙授课程', '蒙授课程'),
    ('特岗教师', '特岗教师'),
    ('六项人员', '六项人员'),
    ('军队文职', '军队文职'),
    ('遴选', '遴选'),
    ('社区', '社区'),
    ('书记员', '书记员'),
    ('专升本', '专升本'),
    ('社会', '社会'),
    ('线上加盟会', '线上加盟会'),
    ('其他', '其他'),
]
# 课程分类
COURSE_CATEGORY_LIST = [
    ('直播课', '直播课'),
    ('录播课', '录播课'),
    ('其他', '其他'),
]
# 讲课教师
TEACHER_LIST = [
    ('刘一凡', '刘一凡'),
    ('张宇', '张宇'),
    ('柴一奘', '柴一奘'),
    ('王语涵', '王语涵'),
    ('苏雅', '苏雅'),
    ('周信', '周信'),
]


# Create your models here.
class Media(models.Model):
    media_icon = models.ImageField(upload_to='icon/', verbose_name='媒体图标', blank=True, null=True, help_text='请设置媒体图标')
    media_file = models.FileField(upload_to='./', max_length=500, verbose_name='媒体文件', blank=True, null=True,
                                  help_text='请选择上传文件')
    media_name = models.CharField(max_length=512, verbose_name='媒体名称', blank=True, help_text='默认为媒体文件名，亦可手动设置')
    media_size = models.CharField(max_length=135, verbose_name='媒体大小', blank=True)
    media_type = models.CharField(max_length=135, choices=MEDIA_TYPE_LIST, verbose_name='媒体类型', default='视频')
    media_category = models.CharField(max_length=135, choices=MEDIA_CATEGORY_LIST, blank=True, null=True,
                                      verbose_name='媒体分类')
    course_category = models.CharField(max_length=135, choices=COURSE_CATEGORY_LIST, blank=True, null=True,
                                       verbose_name='课程分类')
    course_time = models.CharField(max_length=135, blank=True, null=True, verbose_name='课时')
    course_teachers = models.CharField(max_length=512, blank=True, null=True, verbose_name='讲课教师')
    course_start_time = models.DateTimeField(blank=True, null=True, verbose_name='开课时间')
    media_tags = TaggableManager(blank=True, verbose_name='标签', help_text='请输入标签，多个标签以空格或英文逗号分隔')
    media_remark = models.CharField(max_length=512, blank=True, null=True, verbose_name='备注')
    media_creator = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, verbose_name='媒体创建者')
    media_creation_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    media_modification_time = models.DateTimeField(auto_now=True, verbose_name='修改时间')
    last_editor = models.CharField(max_length=135, verbose_name='最后编辑者')

    class Meta:
        db_table = 'mms'
        verbose_name = '媒体'
        verbose_name_plural = '媒体'
        ordering = ['-media_modification_time']

    def __str__(self):
        return self.media_name

    @property
    def filesize(self):
        x = self.media_file.size
        y = 512000
        if x < y:
            value = round(x / 1024, 2)
            ext = ' KB'
        elif x < y * 1024:
            value = round(x / (1024 * 1024), 2)
            ext = ' MB'
        else:
            value = round(x / (1024 * 1024 * 1024), 2)
            ext = ' GB'
        return str(value) + ext
